﻿using Microsoft.AspNetCore.Mvc;
using WCS_Entity.Dto;
using WCS_Entity.Dto.ComProtocolDto;
using WCS_Service.Interface;

namespace WCS.Controllers
{
    /// <summary>
    /// 自动化设备通信协议管理接口
    /// </summary>
    [ApiController]
    [Route("api/[controller]/[action]")]
    public class ComProtocolController : ControllerBase
    {
        private readonly IComProtocolService _comProtocolService;

        public ComProtocolController(IComProtocolService comProtocolService)
        {
            _comProtocolService = comProtocolService;
        }
        #region 通信协议管理
        /// <summary>
        /// 查询指定通信协议
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<WebApiCallBack> QueryComProtocolAsync(int id)
        {
            var result = new WebApiCallBack();
            var dtoInfo = await _comProtocolService.QueryComProtocolAsync(id);
            if (dtoInfo is null)
            {
                result.status = false;
                result.msg = "自动化设备通信协议获取失败";
                return result;
            }
            result.data = dtoInfo;
            return result;
        }
        /// <summary>
        /// 查询所有自动化设备通信协议
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<WebApiCallBack> QueryAllComProtocolAsync()
        {
            var result = new WebApiCallBack();
            var dtoInfos = await _comProtocolService.QueryAllComProtocolAsync();
            if (dtoInfos is null)
            {
                result.status = false;
                result.msg = "自动化设备所有通信协议获取失败";
                return result;
            }
            result.data = dtoInfos;
            return result;
        }
        /// <summary>
        /// 删除指定通信协议
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete]
        public async Task<WebApiCallBack> DeleteComProtocolAsync(int id)
        {
            var result = new WebApiCallBack();
            result.status = await _comProtocolService.DeleteComProtocol(id);
            result.msg = result.status ? "删除成功":"删除失败";
            return result;
        }
        /// <summary>
        /// 创建自动化设备通信协议
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<WebApiCallBack> CreateComProtocolAsync([FromBody] CreateComProtocolDto dto)
        {
            var result = new WebApiCallBack();
            if (string.IsNullOrEmpty(dto.protocolType))
            {
                result.status = false;
                result.msg = "通信协议类型或内容不能为空";
                return result;
            }
            var dtoInfo = await _comProtocolService.CreateComProtocolAsync(dto);
            if (dtoInfo is null)
            {
                result.status = false;
                result.msg = "自动化设备通信协议创建失败";
                return result;
            }
            result.data = dtoInfo;
            return result;
        }
        /// <summary>
        /// 更新自动化设备通信协议
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<WebApiCallBack> UpdateComProtocolAsync([FromBody] UpdateComProtocolDto dto)
        {
            var result = new WebApiCallBack();
            var dtoInfo = await _comProtocolService.UpdateComProtocolAsync(dto);
            if (dtoInfo is null)
            {
                result.status = false;
                result.msg = "自动化设备通信协议更新失败";
                return result;
            }
            result.data = dtoInfo;
            return result;
        }
        /// <summary>
        /// 查询所有系统支持的报文类型
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<WebApiCallBack> QueryAllMsgType()
        {
            var result = new WebApiCallBack();
            var info = await _comProtocolService.QueryProtocolMsgTypeAsync();
            if (info is null)
            {
                result.status = false;
                result.msg = "系统支持的报文类型查询失败";
                return result;
            }
            result.data = info;
            return result;
        }
        #endregion
    }
}
